﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Customer360AutoJob.Core.Configuration;
using Customer360AutoJob.Models.ViewModels.ExportDataToSmartSME;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using WinSCP;

namespace Customer360AutoJob.Jobs
{
    public class ExportCustomerInfo : JobBase, IJob
    {
        #region Properties
        public UserRecord CurrentUserRecord { get; set; }

        #endregion
        public void Execute(IJobExecutionContext context)
        {
            try
            {

                CurrentUserRecord = new UserRecord
                {
                    UserName = "admin",
                    GroupIdentifier = "1"
                };
                var listCustomer = RetrieveListCustomer();
                DataTable dt = new DataTable();
                dt = ToDataTable(listCustomer);

                var fileName = "TableCustomerMBD" + DateTime.Now.ToString("dd/MM/yyyy") + ".xlsx";
                fileName = fileName.Replace("/", "");
                ExportToExcel(dt, fileName);
                
            }
            catch (Exception)
            {

                throw;
            }
            Thread.Sleep(100);
        }

        private List<CustomerViewModel> RetrieveListCustomer()
        {
            List<CustomerViewModel> result = new List<CustomerViewModel>();
            try
            {

                var customerRecords = new RetrieveCustomerResponse();
                using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
                {
                    FilterRecord filter = new FilterRecord();
                    filter.IsExport = true;
                    RetrieveCustomerRequest request = new RetrieveCustomerRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.TotalCustomerData"
                        },
                        Page = new PageRecord
                        {
                            PageSize = 0,
                            PageIndex = 0
                        },
                        CurrentUser = CurrentUserRecord,
                        Filter = filter
                    };
                    customerRecords = serviceClient.Proxy.RetrieveTotalCustomer(request);
                    result = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerViewModel>() : customerRecords.Records.Select(x => new CustomerViewModel
                    {
                        CIF = x.CIF,
                        CUS_NAME = x.Name,
                        BRANCH_ID = x.BranchId,
                        ADDRESS = x.Address,
                        CUS_OPEN_DATE = x.DateOpenCif,
                        BIRTH_INCORP_DATE = x.BirthIncorpDate.HasValue ? x.BirthIncorpDate.Value : DateTime.MinValue,
                        LEGAL_ID = x.LegalId,
                        VPB_INDUSTRY = x.IndustryCode.Value,
                        INDUSTRY_NAME = x.VpbIndustry,
                        DOANH_THU = x.DoanhThu,
                        EMAIL_ADDR = x.EmailAddress,
                        SEGMENT = x.SubSegment,
                        DAO = x.Dao,
                        CUST_TYPE = x.CustTypeCode.HasValue ? x.CustTypeCode.Value.ToString() : string.Empty,
                        CUST_TYPE_NAME = x.CustTypeName,
                        INDUSTRY_NAME_EN = x.VpbIndustryEn,
                        DAO_NAME = x.DaoName,
                        CUS_STATUS = x.CusStatus,
                        BUSINESS_DATE = DateTime.ParseExact(x.BusinessDate, "yyyyMMdd", CultureInfo.InvariantCulture),
                        MODIFIED_DATE = x.ModifiedDate
                    }).ToList();
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}